package it.gestionedocumentale.hibernate.dao;

import it.gestionedocumentale.model.Profilo;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

public class ProfiloDAO extends BaseHibernateDAO {
	
	// property constants
	public static final String NOME = "nome";
	public static final String COGNOME = "cognome";
	public static final String EMAIL = "email";

	public void save(Profilo transientInstance) throws Exception{
		getSession().save(transientInstance);
	}
	
	public void update(Profilo transientInstance) throws Exception{
		getSession().saveOrUpdate(transientInstance);
	}

	public void delete(Profilo persistentInstance) throws Exception{
		getSession().delete(persistentInstance);
	}

	public Profilo findById(Long id) throws Exception{
		Profilo instance = (Profilo) getSession().get(Profilo.class, id);
		return instance;
	}

	@SuppressWarnings("unchecked")
	public List<Profilo> findByExample(Profilo instance) throws Exception{
		Criteria cri = getSession().createCriteria(Profilo.class);
		cri.add(Example.create(instance));
		List<Profilo> results = cri.list();
		return results;
	}
	
	@SuppressWarnings("unchecked")
	public List<Profilo> findAll() throws Exception{
		String queryString = "from Profilo";
		Query queryObject = getSession().createQuery(queryString);
		return queryObject.list();
	}

	@SuppressWarnings("unchecked")
	public List<Profilo> findByProperty(String propertyName, Object value) throws Exception{
		String queryString = "from Profilo as model where model." + propertyName
				+ "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, value);
		return queryObject.list();
	}
	
	public List<Profilo> findByNome(Object nome) throws Exception{
		return findByProperty(NOME, nome);
	}
	
	public List<Profilo> findByCognome(Object cognome) throws Exception{
		return findByProperty(COGNOME, cognome);
	}
	
	public List<Profilo> findByEmail(Object email) throws Exception{
		return findByProperty(EMAIL, email);
	}

	public Profilo merge(Profilo detachedInstance) throws Exception{
		Profilo result = (Profilo) getSession().merge(detachedInstance);
		return result;
	}


}
